import { CommandBase } from "./command/base";

class TrieNode {
    children: { [key: string]: TrieNode };
    suggestions: { name: string, detail: string, docs: string, insert: string, tags: string[], mode: number }[];
    constructor() {
        this.children = {};
        this.suggestions = [];
    }
}

class Trie {
    root: TrieNode
    constructor() {
        this.root = new TrieNode()
    }

    insert(word: string, suggestions: { name: string, detail: string, insert: string, docs: string, tags: string[], mode: number }): void {
        let node = this.root;
        for (const char of word) {
            if (!node.children[char]) {
                node.children[char] = new TrieNode();
            }
            node = node.children[char];
            if (!node.suggestions.includes(suggestions)) {
                node.suggestions.push(suggestions)
            }
        }
    }

    search(prefix: string): { name: string, detail: string, insert: string, docs: string, tags: string[], mode: number }[] {
        let node = this.root;
        for (const char of prefix) {
            if (!node.children[char]) {
                return [];
            }
            node = node.children[char];
        }
        return node.suggestions;
    }

    insertCommand(cmd: CommandBase) {
        this.insert(cmd.name, cmd)
        cmd.tags.forEach((e) => {
            this.insert(e, cmd)
        })
    }
}

const trie = new Trie();
export default trie;
import "./command/account";
import "./command/check";
import "./command/handle";
import "./command/item";
import "./command/move";
import "./command/other";
import "./command/pet";
import "./command/talk";
import "./command/task";
import "./command/team";
import "./command/transmit";
import "./command/warehouse";
import "./command/dict";